<template>
	<view class="box">
		<view class="phone-title">
			
			当前账号{{currentPhone}}
		</view>
		<view class="phone-input phone-input1">
			<u--input placeholder="请您输入手机号码" border="none" v-model="numberphone" @change="change" clearable></u--input>
		</view>
		<view class="phone-input">
			<u-input placeholder="验证码" border="none" v-model="yzmCode" @change="change" clearable>
				<template slot="suffix">
					<view class="yzm-btn">
						<u-code ref="uCode" @change="codeChange" seconds="120" changeText="X秒重新获取"
							style="background: #fff;color: #0F53AC;border: none;"></u-code>
						<u-button :disabled="btnDisabled" @click="getCode" :text="tips" size="normal" color="#0F53AC">
						</u-button>
					</view>
				</template>
			</u-input>
		</view>
		<view class="comfir-btn">
			<view class="qd-btn" @click="updateNumberPhone">
				确&nbsp;&nbsp;定
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tips: '',
				numberphone: '', //手机号码
				yzmCode: '', //验证码
				currentPhone: '',
				btnDisabled: false
			}
		},
		onLoad(options) {
			this.currentPhone = options.currentphone || '';
		},
		methods: {
			codeChange(text) {
				console.log(text)
				this.tips = text;
				if (text === '重新获取') {
					this.btnDisabled = false
				}
			},
			getCode() {
				if (!this.numberphone) {
					uni.showToast({
						icon: 'none',
						title: '请您先输入手机号码！'
					})
					return
				}
				let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/; //正则表达式定义手机号正确格式
				if (!reg.test(this.numberphone)) { //判断手机号格式时候正确
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					//
					uni.showLoading({
						title: '正在获取验证码'
					})
					uni.$u.http.post(`/base/mobile/userInfo/sendSmsCode`, {
						userAccount: this.numberphone
					}).then(res => {
						if (res.code == 0) {
							setTimeout(() => {
								uni.hideLoading();
								// 这里此提示会被this.start()方法中的提示覆盖
								uni.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();
								this.btnDisabled = true
							}, 500);
						}
					}).catch(err => {})

				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			},
			//提交
			updateNumberPhone() {
				
				if (!this.numberphone) {
					uni.showToast({
						icon: 'none',
						title: '手机号码不能为空！'
					})
					return
				}
				let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/; //正则表达式定义手机号正确格式
				if (!reg.test(this.numberphone)) { //判断手机号格式时候正确
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				if (!this.yzmCode) {
					uni.showToast({
						icon: 'none',
						title: '验证码不能为空！'
					})
					return
				}
				uni.$u.http.post(`/base/mobile/userInfo/updateUserAccount`, {
					userAccount: this.numberphone,
					smsCode: this.yzmCode
				}).then(res => {
					if (res.code == 0) {
						uni.navigateTo({
							url: '/pages/subPackagesD/mycenter/phonesucess'
						})
					}
				}).catch(err => {})
			}
		}
	}
</script>

<style lang="scss">
	page {	
		background: #ffffff;
	}
	.box{
		padding: 48rpx 32rpx;
	}

	.phone-title {
		font-size: 28rpx;
		color: #8C8C8C;
	}

	.phone-input {
		margin-top: 40rpx;
		height: 96rpx;
		background: #F2F2F2;
		border-radius: 12rpx;
		display: flex;
		align-items: center;
		.yzm-btn{
			width: 120px;
			height: 96rpx;
		}
		.u-input{
			margin-left: 20rpx;
			height: 96rpx;
			font-size: 32rpx;
			.u-button{
				height: 96rpx;
				font-size: 32rpx;
			}
			.u-input_content{
				margin-right: 20rpx;
			}
		}
	}
	.phone-input1{
		.u-input{
			margin-right: 20rpx;
		}
		.u-input__content__subfix-icon{
			border-radius: 12rpx;
		}
	}

	.comfir-btn{
		width: 100%;
		height: 96rpx;
		margin-top: 64rpx;
		.qd-btn{
			width: 100%;
			height: 96rpx;
			line-height: 96rpx;
			border-radius: 12rpx;
			background: #0F53AC;
			color: #fff;
			font-size: 34rpx;
			text-align: center;
		}
	}
</style>
